#include <e32std.h>
#include "S60_Sound.h"
#include "StdAfx.h"
#include <Mda\Common\Resource.h>

CSoundPlayer* CSoundPlayer::NewL(const TDesC& aFile)
{
    CSoundPlayer* self = NewLC(aFile);
    CleanupStack::Pop(self);
    return self;
}


CSoundPlayer* CSoundPlayer::NewLC(const TDesC& aFile)
{
    CSoundPlayer* self = new (ELeave) CSoundPlayer();
    CleanupStack::PushL(self);
    self->ConstructL(aFile);
    return self;
}

CSoundPlayer::CSoundPlayer()
{
	m_nPlay = 0;
}


CSoundPlayer::~CSoundPlayer()
{
    delete iMdaPlayer;    
    iMdaPlayer = NULL;
}


void CSoundPlayer::ConstructL(const TDesC& aFile)
{
	//
	// Create a file audio player utility instance 
	//
	iMdaPlayer=CMdaAudioPlayerUtility::NewFilePlayerL(aFile,*this);
}



void CSoundPlayer::Play(bool bLoop /* = false */)
{
	m_nPlay = 1;
	if(bLoop)
	{
		m_nPlay = 2;
	}
    if(iState==EReady)
    {
        iState=EPlaying;
		if(bLoop)
		{
			TTimeIntervalMicroSeconds trailingSilence(TInt64(0));
			iMdaPlayer->SetRepeats(KMdaRepeatForever , trailingSilence); 
		}
        iMdaPlayer->Play();
    }
}


void CSoundPlayer::Stop()
{
	m_nPlay = 0;
    if(iState==EPlaying)
    {
        iMdaPlayer->Stop();
        iState = EReady;
    }
}


// from MMdaAudioPlayerCallback
void CSoundPlayer::MapcInitComplete(TInt aError, const TTimeIntervalMicroSeconds& /*aDuration*/)
{
    iState = aError ? ENotReady : EReady;
    if(iState == EReady)
	{
		if(m_nPlay > 1)
		{
			TTimeIntervalMicroSeconds trailingSilence(TInt64(0));
			iMdaPlayer->SetRepeats(KMdaRepeatForever , trailingSilence); 

			Play();
		}
		else if(m_nPlay > 0)
		{
			Play();
		}
	}
}


void CSoundPlayer::MapcPlayComplete(TInt aError)
{
    iState = aError ? ENotReady : EReady;
    if(iState == EReady)
    {
    }    
}

